﻿using DevExpress.XtraGrid.Views.Base;
using Extensions;
using OTS.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace OTS
{
    public partial class OdemeTablosu : XForm
    {
        public OdemeTablosu()
        {
            InitializeComponent();
        }

        private void OdemeTablosu_Load(object sender, EventArgs e)
        {
            deBaslangic.DateTime = DateTime.Now.AddMonths(-1);
            deBitis.DateTime = DateTime.Now;
            GetReport();
        }

        DataSet dsData;
        private void GetReport()
        {
            if (deBaslangic.DateTime > deBitis.DateTime)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("Tarih aralığınızı kontrol edin. Başlangıç tarihi, bitiş tarihinden sonra olamaz.", "Uyarı", MessageBoxButtons.OK);
                return;
            }

            this.Cursor = Cursors.WaitCursor;

            int currentRowIndex = Convert.ToInt32(gvSum.GetFocusedRowCellValue("HareketTipiId"));

            dsData = AppSession.DataManager.ExecuteDataSet("GetOdemeTablosu", AppSession.UserInfo.SirketId, deBaslangic.DateTime, deBitis.DateTime);
            gvSum.Init(dsData.Tables[0]);

            gvSum.AddSummary();

            if (AppSession.UserInfo.SirketId == 1)
            {
                gvSum.AddColumn("DiğerToplam", typeof(decimal), "Toplam-ISNULL(Karbel,0)");
                gvSum.MakeColumnInvisible("Karbel Dış", "Ersur", "Sibel", "Kartopu", "Aker");
                gvSum.Columns["Karbel Dış"].OptionsColumn.ShowInCustomizationForm = gvSum.Columns["Ersur"].OptionsColumn.ShowInCustomizationForm = gvSum.Columns["Sibel"].OptionsColumn.ShowInCustomizationForm = gvSum.Columns["Kartopu"].OptionsColumn.ShowInCustomizationForm = gvSum.Columns["Aker"].OptionsColumn.ShowInCustomizationForm = false;
            }
            else if (AppSession.UserInfo.SirketId == 2)
            {
                gvSum.AddColumn("DiğerToplam", typeof(decimal), "Toplam-ISNULL(Karbel Dış,0)");
                gvSum.MakeColumnInvisible("Karbel", "Ersur", "Sibel", "Kartopu", "Aker");
                gvSum.Columns["Karbel"].OptionsColumn.ShowInCustomizationForm = gvSum.Columns["Ersur"].OptionsColumn.ShowInCustomizationForm = gvSum.Columns["Sibel"].OptionsColumn.ShowInCustomizationForm = gvSum.Columns["Kartopu"].OptionsColumn.ShowInCustomizationForm = gvSum.Columns["Aker"].OptionsColumn.ShowInCustomizationForm = false;
            }

            else if (AppSession.UserInfo.SirketId == 3)
            {
                gvSum.AddColumn("DiğerToplam", typeof(decimal), "Toplam-ISNULL(Ersur,0)");
                gvSum.MakeColumnInvisible("Karbel", "Karbel Dış", "Sibel", "Kartopu", "Aker");
                gvSum.Columns["Karbel"].OptionsColumn.ShowInCustomizationForm = gvSum.Columns["Karbel Dış"].OptionsColumn.ShowInCustomizationForm = gvSum.Columns["Sibel"].OptionsColumn.ShowInCustomizationForm = gvSum.Columns["Kartopu"].OptionsColumn.ShowInCustomizationForm = gvSum.Columns["Aker"].OptionsColumn.ShowInCustomizationForm = false;
            }

            else if (AppSession.UserInfo.SirketId == 4)
            {
                gvSum.AddColumn("DiğerToplam", typeof(decimal), "Toplam-ISNULL(Aker,0)");
                gvSum.MakeColumnInvisible("Karbel", "Karbel Dış", "Sibel", "Kartopu", "Ersur");
                gvSum.Columns["Karbel"].OptionsColumn.ShowInCustomizationForm = gvSum.Columns["Karbel Dış"].OptionsColumn.ShowInCustomizationForm = gvSum.Columns["Sibel"].OptionsColumn.ShowInCustomizationForm = gvSum.Columns["Kartopu"].OptionsColumn.ShowInCustomizationForm = gvSum.Columns["Ersur"].OptionsColumn.ShowInCustomizationForm = false;
            }

            else if (AppSession.UserInfo.SirketId == 5)
            {
                gvSum.AddColumn("DiğerToplam", typeof(decimal), "Toplam-ISNULL(Sibel,0)");
                gvSum.MakeColumnInvisible("Karbel", "Karbel Dış", "Aker", "Kartopu", "Ersur");
                gvSum.Columns["Karbel"].OptionsColumn.ShowInCustomizationForm = gvSum.Columns["Karbel Dış"].OptionsColumn.ShowInCustomizationForm = gvSum.Columns["Aker"].OptionsColumn.ShowInCustomizationForm = gvSum.Columns["Kartopu"].OptionsColumn.ShowInCustomizationForm = gvSum.Columns["Ersur"].OptionsColumn.ShowInCustomizationForm = false;
            }

            else if (AppSession.UserInfo.SirketId == 6)
            {
                gvSum.AddColumn("DiğerToplam", typeof(decimal), "Toplam-ISNULL(Kartopu,0)");
                gvSum.MakeColumnInvisible("Karbel", "Karbel Dış", "Aker", "Sibel", "Ersur");
                gvSum.Columns["Karbel"].OptionsColumn.ShowInCustomizationForm = gvSum.Columns["Karbel Dış"].OptionsColumn.ShowInCustomizationForm = gvSum.Columns["Aker"].OptionsColumn.ShowInCustomizationForm = gvSum.Columns["Sibel"].OptionsColumn.ShowInCustomizationForm = gvSum.Columns["Ersur"].OptionsColumn.ShowInCustomizationForm = false;
            }

            gvSum.SetCaptions("HareketTipiId:#");
            gvSum.FormatAsNumber(2, "Aker", "Sibel", "Kartopu", "Karbel", "Karbel Dış", "Ersur", "DiğerToplam", "Toplam");

            if (AppSession.UserInfo.SirketId > 0)
                gvSum.BringSideBySide("DiğerToplam:Toplam");

            gvDetail.Init(dsData.Tables[1].Copy());
            gvDetail.AddSummary();
            gvDetail.FormatAsNumber(2, "Miktar", "IslemTarihindekiKur", "IslemTarihindekiTL", "ReelKur", "ReelTL");

            gvDetail.MakeColumnInvisible("SirketId", "HareketTipiId");
            gvDetail.SetCaptions("OdemeId:#", "Aciklama:Açıklama", "IslemTarihindekiKur:Giriş Kur", "IslemTarihindekiTL:Giriş TL", "Parabirimi:PB");
            gvDetail.FormatAsDate("Tarih");
            gvDetail.AddSummary("IslemTarihindekiTL", "ReelTL");
            // gvDetail.ColumnPanelRowHeight = 32;
            gvDetail.Appearance.HeaderPanel.TextOptions.WordWrap = DevExpress.Utils.WordWrap.Wrap;
            gvDetail.OptionsView.EnableAppearanceEvenRow = true;

            gvSum.BestFitColumns();
            gvDetail.BestFitColumns();
            this.Cursor = Cursors.Default;



            gvSum.FocusedRowChanged += gvSum_FocusedRowChanged;
            gvSum.FocusedColumnChanged += gvSum_FocusedColumnChanged;

            gvSum.OptionsView.EnableAppearanceEvenRow = true;

            gvSum.CustomDrawCell += gvSum_CustomDrawCell;
            gvDetail.CustomDrawCell += gvDetail_CustomDrawCell;

            Sirketler.AddRange(new string[] { "Karbel", "Karbel Dış", "Ersur", "Aker", "Sibel", "Kartopu" });

            gvSum.FocusedRowHandle = gvSum.LocateByValue(0, gvSum.Columns["HareketTipiId"], currentRowIndex);

        }

        void gvDetail_CustomDrawCell(object sender, RowCellCustomDrawEventArgs e)
        {
            if (new string[] { "Miktar", "IslemTarihindekiTL", "ReelTL" }.Contains(e.Column.FieldName) && e.CellValue.ToIntNN() < 0)
            {
                //e.DisplayText = e.DisplayText.Replace("-","");
                e.Appearance.ForeColor = Color.Red;
            }
        }

        void gvSum_CustomDrawCell(object sender, RowCellCustomDrawEventArgs e)
        {
            if ((Sirketler.Contains(e.Column.FieldName) || "Toplam".Contains(e.Column.FieldName)) && e.CellValue.ToIntNN() < 0)
            {
                //e.DisplayText = e.DisplayText.Replace("-","");
                e.Appearance.ForeColor = Color.Red;
            }
        }


        void gvSum_FocusedColumnChanged(object sender, FocusedColumnChangedEventArgs e)
        {
            refresh();
        }

        bool internalChanging = false;

        void refresh()
        {
            if (internalChanging)
                return;
            internalChanging = true;
            BeginInvoke(new MethodInvoker(delegate
            {
                ViewFocusedCellChanged();
                internalChanging = false;
            }));

        }

        List<string> Sirketler = new List<string>();

        void ViewFocusedCellChanged()
        {
            string col = gvSum.FocusedColumn.FieldName;
            string htipi = gvSum.GetFocusedRowCellValue("HareketTipiId").ToStringNN();
            if (htipi == "")
                return;

            string sirketId = "";
            switch (col)
            {
                case "Karbel":
                    sirketId = "1";
                    break;
                case "Karbel Dış":
                    sirketId = "2";
                    break;
                case "Ersur":
                    sirketId = "3";
                    break;
                case "Aker":
                    sirketId = "4";
                    break;
                case "Sibel":
                    sirketId = "5";
                    break;
                case "Kartopu":
                    sirketId = "6";
                    break;
                case "DiğerToplam":
                    sirketId = "0";
                    break;
                case "Toplam":
                    if (AppSession.UserInfo.SirketId != -1)
                        sirketId = "null";
                    else
                        sirketId = "SirketId";
                    break;
                default:
                    sirketId = "SirketId";
                    break;
            }

            string filter = "HareketTipiId=" + htipi + " and ISNULL(SirketId,0)=" + sirketId;
            if (col == "Toplam")
                filter = "HareketTipiId=" + htipi;

            DataTable dt = new DataTable();
            if (dsData.Tables[1].Select(filter).Length > 0)
                dt = dsData.Tables[1].Select(filter).CopyToDataTable();

            DataSet ds = new DataSet();
            ds.Tables.Add(dt);
            gvDetail.VeriyiGuncelle(dt);
            gvDetail.BestFitColumns();
        }

        void gvSum_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            refresh();
        }



        private void btnSorgula_Click(object sender, EventArgs e)
        {
            GetReport();
        }
    }
}
